package com.zh.rent.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;

public class HttpURLConnectionUtil {

    public String sendGet(String urlParame) throws IOException {
        //创建httpclient实例
        HttpClient httpClient=new HttpClient();
        //设置httpclient连接主机服务器超时时间： 以毫秒为单位 1000ms=1s
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
        //创建get请求方法实例对象
        GetMethod getMethod=new GetMethod(urlParame);
        //设置get请求超时时间，value以毫秒为单位
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,10000);
        //设置请求头
        getMethod.addRequestHeader("Content-Type", "application/json");
        //执行get
        httpClient.executeMethod(getMethod);
        //获取返回数据
        String result=getMethod.getResponseBodyAsString();
        //释放http连接
        getMethod.releaseConnection();

        return result;
    }

    public String mailText(String str){
        String s=null;
        s=str.replaceAll("<style[^>]*?>[\\s\\S]*?<\\/style>","");
        s =s.replaceAll("</?[^>]+>", "");
        s = s.replaceAll("<a>\\s*|\t|\r|\n|</a>", "");
        s=s.replaceAll("&nbsp;","");
        s=s.replaceAll("\\\\n","");
        return s;
    }

    public String mailName(String str){
        int i = str.indexOf("content\":\"");//头
        int i1 = str.indexOf("attachments");//尾
        String substring = str.substring(i +10, i1 - 4);
        return substring;
    }

   /* public static void main(String[] args) {
        try {

            HttpURLConnectionUtil httpURLConnectionUtil = new HttpURLConnectionUtil();
            String s = httpURLConnectionUtil.sendGet("http://127.0.0.1:5658/?type=login&username=haishi13512109@163.com&password=ji919488&proxyip=");

            String s2 = httpURLConnectionUtil.mailText(s);
            String s1 = httpURLConnectionUtil.mailName(s2);
            System.out.println(s);
            System.out.println(s1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
